//管理员列表模块
import axios from 'axios'

const $host = 'http://132.232.110.185/personnelManagement'
export default {
    namespaced: true,
    actions: {
        //修改请求
        async updateInfo(ctx, obj) {
            console.log(obj)
            axios({
                method: 'POST',
                url: `${$host}/user/editUser`,
                headers: {
                    'Content-Type': 'application/json'
                },
                data: {
                    "mEmail": obj.mEmail,
                    "mId": obj.mId,
                    "mLogin": obj.mLogin,
                    "mName": obj.mName,
                    "mNewPassword": obj.mNewPassword,
                    "mNewPassword2": obj.mNewPassword2,
                    "mNo": obj.mNo,
                    "mPassword": obj.mPassword,
                    "mPhone": obj.mPhone,
                    "mSex": obj.mSex,
                }
            }).then(res => {
                if (res.data.msg === '修改成功') ctx.commit('adminUpdate')
            }).catch(err => {
                console.log(err)
            })
        },
        //添加请求
        async addInfo(ctx, obj) {
            console.log(obj)
            axios({
                method: 'POST',
                url: `${$host}/user/addUser`,
                headers: {
                    'Content-Type': 'application/json'
                },
                data: {
                    "mEmail": obj.mEmail,
                    "mLogin": obj.mLogin,
                    "mName": obj.mName,
                    "mNo": obj.mNo,
                    "mPassword": obj.mPassword,
                    "mPhone": obj.mPhone,
                    "mSex": obj.mSex,
                    "rName": obj.rName
                }
            }).then(res => {
                ctx.commit('adminAdd')
            }).catch(err => {
                console.log(err)
            })
        }
    },
    mutations: {
        //关闭模态框
        closeDialog(state) {
            if (state.formLabelAlign.adminAddFormVisible) state.formLabelAlign.adminAddFormVisible = false
            if (state.formLabelAlign.adminUpdateFormVisible) state.formLabelAlign.adminUpdateFormVisible = false
        },
        //修改管理员dialog
        handleAdminEdit(state, row) {
            state.updateStatus = false
            state.formLabelAlign.adminUpdateFormVisible = true
            state.updateAdmin = {...row, mNewPassword: '', mNewPassword2: ''}
        },
        //修改资料
        adminUpdate(state) {
            state.updateStatus = true
            state.formLabelAlign.adminUpdateFormVisible = false
            console.log('修改成功')
        },
        //点击添加管理员资料显示dialog
        showAdminAddDialog(state) {
            state.addStatus = false
            state.formLabelAlign.adminAddFormVisible = true
            state.addAdmin = {}
        },
        //选值
        changeRoleName(state, value) {
            if (state.formLabelAlign.adminAddFormVisible) state.addAdmin.aName = value
            if (state.formLabelAlign.adminUpdateFormVisible) state.updateAdmin.aName = value
        },
        //添加新管理员
        adminAdd(state) {
            state.addStatus = true
            state.formLabelAlign.adminAddFormVisible = false
            console.log('添加成功')
        }
    },
    state: {
        formLabelAlign: {
            aAccount: '',
            aRealName: '',
            aOldPwd: '',
            aNewPwd1: '',
            aNewPwd2: '',
            radio: '1',
            aTel: '',
            aName: '',
            aEmail: '',
            adminAddFormVisible: false,
            adminUpdateFormVisible: false,
        },
        addAdmin: {
            aAccount: '',
            aRealName: '',
            aPwd1: '',
            aPwd2: '',
            radio: '1',
            aTel: '',
            aName: '',
            aEmail: '',
            value: ''
        },
        updateAdmin: {},
        //添加状态
        addStatus: false,
        //修改状态
        updateStatus: false
    },
    getters: {}
}